
public class Test26 {
    public static CompileListNode clone(CompileListNode compileListNode){
        if (compileListNode == null) return null;
        cloneNode(compileListNode);
        connectNode(compileListNode);
        return reconnectNodes(compileListNode);
    }

    //复制链表，把复制的节点放在被复制节点的后面
    public static void cloneNode(CompileListNode head){
        CompileListNode node = head;

        while(node != null){
            CompileListNode temp= new CompileListNode();
            temp.val = node.val;
            temp.next = node.next;
            node.next = temp;
            node = temp.next;
        }
    }
    //链接sibling的节点
    public static void connectNode(CompileListNode head){
        CompileListNode node = head;
        while(node != null){
            if (node.sibling != null){
                node.next.sibling = node.sibling.next;
            }
            node = node.next.next;
        }
    }

    public static CompileListNode reconnectNodes(CompileListNode head){
        CompileListNode node = head;
        if (node == null) return  null;
        CompileListNode newHead = node.next;//新节点
        CompileListNode pointer = node.next;//临时节点
        while(node !=null){
            node.next = pointer.next;
            node = node.next;
            if (node == null) break;;
            pointer.next = node.next;
            pointer = pointer.next;
        }
        return newHead;
    }

    public static void printListNode(CompileListNode head){

        while (head!=null){
            System.out.print(head.val +"->");
            head = head.next;
        }
        System.out.println("null");
    }


    public static void main(String[] args) {
        //          -----------------
        //         \|/              |
        //  1-------2-------3-------4-------5
        //  |       |      /|\             /|\
        //  --------+--------               |
        //          -------------------------
        CompileListNode head = new CompileListNode();
        head.val = 1;
        head.next = new CompileListNode();
        head.next.val = 2;
        head.next.next = new CompileListNode();
        head.next.next.val = 3;
        head.next.next.next = new CompileListNode();
        head.next.next.next.val = 4;
        head.next.next.next.next = new CompileListNode();
        head.next.next.next.next.val = 5;

        head.sibling = head.next.next;
        head.next.sibling = head.next.next.next.next.next;
        head.next.next.next.sibling = head.next;

        CompileListNode tmp = head;
        printListNode(head);
        CompileListNode newHead = clone(tmp);
        printListNode(newHead);
    }
}
class CompileListNode{
    int val;
    CompileListNode next;
    CompileListNode sibling;

}